<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>求阶乘</title>
    <script>
      //获取输入值的阶乘
      function getFactorialNum(num) {
        var result = 1;
        var i = 1;
        while (i <= num) {
          result *= i;
          i++;
        }
        // console.log(num + "的阶乘等于" + result);
        return result;
      }
      //求1~n各个数的阶乘和
      //eg: 1! + 2! + 3! + 4! + …… + n!的和
      function getAllFactorialSum(num) {
        var sum = 0;
        var i = 1; //初始值
        while (i <= num) {
          sum += getFactorialNum(i);
          // console.log(sum);
          i++;
        }
        alert(num + "的1! + 2! + 3! + 4! + …… + n!的和等于 " + sum);
      }
      getAllFactorialSum(parseInt(prompt("请输入一个正整数", "6")));
    </script>
  </head>
  <body></body>
</html>
